var ShopWindow = Window.extend({
    _ccsFileName:res.ShopWindow_json,
    _iapUrl: 'http://pay.yayayouxi.com/payServer/order/appleresult',
    _buyIndex: 0,
    _cid: null,
    _keyIap: 'yayagameiapkey_2017YY',
    _interval: null,
    _iapList: [
        {money: 6, card: 1, key: 'xykwx_fangka1'},
        {money: 30, card: 6, key: 'xykwx_fangka6'},
        {money: 128, card: 32, key: 'xykwx_fangka32'}
    ],
    onExit: function () {
        this._super();
        if (this._interval) {
            clearInterval(this._interval);
            this._interval = null;
        }
    },
    /**
     * 初始化UI界面显示
     * @private
     */
    initWindow: function () {
        for (var i = 0; i < 3; i++) {
            this._nodeList['fk' + i + '.card'].setString('房卡' + this._iapList[i].card + '张');
            this._nodeList['fk' + i + '.money'].setString(this._iapList[i].money + '元');
            this._nodeList['fk' + i + '.light'].setVisible(false);
        }
        this._selectCard(this._buyIndex);
    },

    _initClickListeners: function () {
        this._super();
        this._clickListeners = {
            'fk0': '_clickRoomCard',
            'fk1': '_clickRoomCard',
            'fk2': '_clickRoomCard',
            'buy': '_clickBuy'
        };
    },

    /**
     * 点击房卡处理
     * @private
     */
    _clickRoomCard: function (sender) {
        var index = sender.getName().substr(-1);
        this._selectCard(index);
    },

    /**
     * 点击购买处理
     * @param sender
     * @protected
     */
    _clickBuy: function (sender) {
        // console.log('购买项：' + JSON.stringify(this._iapList[this._buyIndex]));
        if (!cc.sys.isNative) {
            return;
        }
        showLoading('正在请求服务器..');
        var cid = this._iapList[this._buyIndex]['key'];
        this._cid = cid;
        IAPUtil.buy(cid);
        this._interval = setInterval(this._requestServer.bind(this), 100);
    },

    /**
     * 设置选中的房卡
     * @private
     */
    _selectCard: function (index) {
        this._nodeList['buynum'].setString(this._iapList[index].card);
        this._nodeList['money'].setString(this._iapList[index].money + '元');

        this._nodeList['fk' + this._buyIndex + '.light'].setVisible(false);
        this._nodeList['fk' + this._buyIndex].setScale(1);
        this._nodeList['fk' + index + '.light'].setVisible(true);
        this._nodeList['fk' + index].setScale(1.1);
        this._buyIndex = index;
    },

    /**
     * 请求购买房卡
     * @private
     */
    _requestServer: function () {
        var code = IAPUtil.getResultCode();
        if (code == 'sucess') {
            IAPUtil.setResultCode('');
            if (this._interval) {
                clearInterval(this._interval);
                this._interval = null;
            }
            var ticket = IAPUtil.getResult();
            if (ticket == '' || ticket == null) return;

            var data = {
                ticket: ticket,
                playerid: gameData.uid,
                area: app.area,
                appid: app.appId,
                cid: this._cid,
                timestamp: getCurTimestamp()
            };
            data.sign = Crypto.MD5(data.appid + data.area + data.cid + data.playerid + data.timestamp + this._keyIap);
            http.post(this._iapUrl, data, function (response) {
                hideLoading();
                IAPUtil.setResult('');
                if (response.status == 0) {
                    //购买成功
                    showAlert('支付成功');
                    // gameData.numOfCards[1] += this._iapList[this._buyIndex]['card'];
                    setTimeout(function () {
                        network.send(MsgCode.ReqFangKa, {});
                    }, 2000);
                } else {
                    showAlert('支付失败' + response.status);
                }
            }, function () {
                hideLoading();
                IAPUtil.setResult('');
            }, true);
        } else if (code == 'fail') {
            IAPUtil.setResultCode('');
            if (this._interval) {
                clearInterval(this._interval);
                this._interval = null;
            }
            IAPUtil.setResult('');
            hideLoading();
        }
    }
});